package com.taobao.txc.common.config;

import com.taobao.diamond.client.Diamond;
import com.taobao.diamond.client.impl.ClientWorker;
import com.taobao.diamond.client.impl.DiamondEnv;
import com.taobao.txc.common.LoggerInit;
import com.taobao.txc.common.LoggerWrap;
import com.taobao.txc.common.b.b;
import com.taobao.txc.common.b.c;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.ConnectException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.concurrent.atomic.AtomicInteger;
import org.apache.commons.lang.StringUtils;

public abstract class i
  implements a
{
  protected DiamondEnv a;
  protected static int b = 1000;
  private static final LoggerWrap k = LoggerInit.logger;
  public static a c = null;
  public static a d = null;
  protected static a e = null;
  public static Set<String> f;
  private static Map<String, String> l = new HashMap();
  protected LinkedHashSet<String> g = new LinkedHashSet();
  protected int h = -1;
  protected String i = null;
  private static int m = 50;
  protected AtomicInteger j = new AtomicInteger(b);
  private static Method n = null;
  
  public static a b()
  {
    if (e == null) {
      synchronized (i.class)
      {
        if ((e == null) && (d == null)) {
          try
          {
            c = new f();
            e = c;
          }
          catch (c localc)
          {
            if (localc.b() == b.r)
            {
              o.a(true);
              k.warn(String.format("diamond center failed, switch to console config", new Object[0]));
            }
            else
            {
              throw localc;
            }
          }
        }
      }
    }
    return e;
  }
  
  public boolean a(String paramString1, String paramString2, String paramString3)
  {
    return this.a.publishSingle(paramString1, paramString2, paramString3);
  }
  
  public void b(String paramString1, String paramString2, n paramn)
  {
    this.a.addListeners(paramString1, paramString2, Arrays.asList(new j[] { new j(this, paramn) }));
  }
  
  public void a(String paramString1, String paramString2, n paramn)
  {
    this.a.removeListener(paramString1, paramString2, new k(this, paramn));
  }
  
  public String a(String paramString1, String paramString2)
  {
    long l1 = 5000L;
    for (;;)
    {
      try
      {
        String str = a(paramString1, paramString2, l1);
        return str;
      }
      catch (IOException localIOException)
      {
        k.a(b.a.bd, b.a.be, new Object[] { localIOException.getMessage() });
        try
        {
          Thread.sleep(1000L);
        }
        catch (InterruptedException localInterruptedException) {}
      }
    }
  }
  
  public String a(String paramString1, String paramString2, long paramLong)
  {
    String str = this.a.getConfig(paramString1, paramString2, paramLong);
    if (this.j.incrementAndGet() > b) {
      synchronized (this)
      {
        if (this.j.get() > b)
        {
          this.j.set(0);
          str = b(paramString1, paramString2, paramLong);
        }
      }
    }
    k.info(String.format("dataid:%s,group:%s,result:%s", new Object[] { paramString1, paramString2, str }));
    return str;
  }
  
  public static void a(String paramString1, String paramString2, m paramm)
  {
    String str = com.taobao.txc.common.util.c.g.b(paramString1, paramString2);
    if (f == null) {
      f = new HashSet();
    }
    if (f.contains(str)) {
      return;
    }
    if ((e == null) || (!e.a())) {
      return;
    }
    synchronized (f)
    {
      if (f.contains(str)) {
        return;
      }
      f.add(str);
      b().b(paramString1, paramString2, new l(paramm, paramString1, paramString2, str));
    }
  }
  
  public boolean a()
  {
    return this.a != null;
  }
  
  protected String a(String paramString)
  {
    this.i = paramString;
    StringTokenizer localStringTokenizer = new StringTokenizer(paramString, ",");
    while (localStringTokenizer.hasMoreElements())
    {
      String str = localStringTokenizer.nextToken().trim();
      synchronized (this)
      {
        if (!StringUtils.isEmpty(str)) {
          this.g.add(str);
        }
      }
    }
    if (this.g.size() > 1) {
      this.i = ((String)this.g.iterator().next());
    }
    return this.i;
  }
  
  protected int a(int paramInt)
  {
    this.h = paramInt;
    return this.h;
  }
  
  public String b(String paramString1, String paramString2, long paramLong)
  {
    try
    {
      if (n == null) {
        synchronized (i.class)
        {
          if (n == null)
          {
            n = ClientWorker.class.getDeclaredMethod("getServerConfig", new Class[] { DiamondEnv.class, String.class, String.class, Long.TYPE });
            n.setAccessible(true);
          }
        }
      }
      return (String)n.invoke(null, new Object[] { this.a, paramString1, paramString2, Long.valueOf(paramLong) });
    }
    catch (InvocationTargetException localInvocationTargetException)
    {
      if ((localInvocationTargetException.getTargetException() instanceof ConnectException))
      {
        if (this.g.size() > 1) {
          try
          {
            String str = c(paramString1, paramString2, paramLong);
            return str;
          }
          catch (Exception localException2)
          {
            k.a(b.n.bd, b.n.be, new Object[] { localException2.getMessage() });
          }
        }
        this.j.set(b);
      }
    }
    catch (Exception localException1) {}
    return null;
  }
  
  private String c(String paramString1, String paramString2, long paramLong)
  {
    int i1 = 0;
    String str1 = b(this.i);
    StringBuffer localStringBuffer = new StringBuffer();
    while ((!StringUtils.isEmpty(str1)) && (!str1.equals(this.i)) && (i1 < m))
    {
      DiamondEnv localDiamondEnv;
      if (this.h == -1) {
        localDiamondEnv = new DiamondEnv(new String[] { str1 });
      } else {
        localDiamondEnv = h.a(this.h, new String[] { str1 });
      }
      try
      {
        String str2 = (String)n.invoke(null, new Object[] { localDiamondEnv, paramString1, paramString2, Long.valueOf(paramLong) });
        synchronized (this)
        {
          this.i = str1;
          this.a = localDiamondEnv;
        }
        return str2;
      }
      catch (Exception localException)
      {
        localStringBuffer.append(str1).append(' ');
        str1 = b(str1);
        i1++;
      }
    }
    throw new RuntimeException(localStringBuffer.toString());
  }
  
  private String b(String paramString)
  {
    if (this.g.size() <= 1) {
      return null;
    }
    Iterator localIterator = this.g.iterator();
    Object localObject1 = null;
    int i1 = 0;
    synchronized (this)
    {
      while (localIterator.hasNext())
      {
        String str1 = (String)localIterator.next();
        if (localObject1 == null) {
          localObject1 = str1;
        }
        if (StringUtils.isEmpty(paramString)) {
          return localObject1;
        }
        if (str1.equals(paramString))
        {
          if (localIterator.hasNext())
          {
            String str2 = (String)localIterator.next();
            return str2;
          }
          return localObject1;
        }
        if (i1 > m) {
          return null;
        }
        i1++;
      }
    }
    return null;
  }
  
  static
  {
    try
    {
      String str1 = Diamond.getConfig("com.taobao.txc.jvm.opts", "TXC_GROUP", 5000L);
      if (!StringUtils.isEmpty(str1))
      {
        k.info("get txc jvm opts: " + str1);
        Properties localProperties = System.getProperties();
        Map localMap = com.taobao.txc.common.util.c.g.d(str1);
        Iterator localIterator = localMap.keySet().iterator();
        while (localIterator.hasNext())
        {
          String str2 = (String)localIterator.next();
          localProperties.put(str2, localMap.get(str2));
        }
        System.setProperties(localProperties);
      }
    }
    catch (ExceptionInInitializerError localExceptionInInitializerError1) {}catch (Throwable localThrowable1) {}
    try
    {
      d = new g();
      e = d;
    }
    catch (ExceptionInInitializerError localExceptionInInitializerError2)
    {
      k.warn("Init failed on default diamond");
    }
    catch (Throwable localThrowable2)
    {
      k.warn("Init failed on default diamond");
    }
  }
}


/* Location:           C:\Users\ibm\Downloads\txc-yun-sample (1)\txc-yun-sample\lib\txc-client-2.0.72.jar
 * Qualified Name:     com.taobao.txc.common.config.i
 * JD-Core Version:    0.7.0.1
 */